﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using MobileSales.Website.EmployeeService;
using MobileSales.Model.Employees;
using MobileSales.Website.Infrastructure.Messaging;
using System.ServiceModel;
using MobileSales.Model.Exceptions;
using MobileSales.Common.Logger;
using MobileSales.Website.Infrastructure;
using System.Text;
using MobileSales.Website.Localization;

namespace MobileSales.Website.Controllers
{
    [Authorize]
    public class EmployeesController : ControllerBase
    {
        #region Index
        //
        // GET: /Employee/

        public ActionResult Index(int? roleId)
        {
            EmployeeServiceClient employeeService = new EmployeeServiceClient();
            employeeService.AddValidationToken(this.Token);
            Employee[] employees = null;
            if (roleId.HasValue)
                employees = employeeService.GetEmployeeListByRole(roleId.Value);
            else
                employees = employeeService.GetEmployeeList();
            employeeService.Close();
            if (employees == null)
                this.SetMessage(ErrorStrings.ErrorEmployeeList, MessageType.Error);

            return View(employees);
        } 
        #endregion

        #region Details
        //
        // GET: /Employee/Details/5

        public ActionResult Details(int id)
        {
            EmployeeServiceClient employeeService = new EmployeeServiceClient();
            employeeService.AddValidationToken(this.Token);
            Employee employee = employeeService.GetEmployee(id);
            employeeService.Close();
            if (employee == null)
            {
                this.SetMessage(ErrorStrings.ErrorEmployee, MessageType.Error);
            }
            return View(employee);
        } 
        #endregion

        #region Create
        //
        // GET: /Employee/Create

        public ActionResult Create()
        {
            return View();
        }

        //
        // POST: /Employee/Create

        [HttpPost]
        public ActionResult Create(Employee employee, int contractId, int roleId, FormCollection form)
        {
            try
            {
                employee.Contract = new Contract { Id = contractId };
                employee.Role = new Role { Id = roleId };
                employee.Params = this.GetParameters(form, Model.Params.ParamType.Employee);
                EmployeeServiceClient employeeService = new EmployeeServiceClient();
                employeeService.AddValidationToken(this.Token);
                employee.Password = MobileSales.Website.Models.AccountMembershipService.CalculateSHA1(employee.Password, Encoding.UTF8);
                employeeService.StoreEmployee(employee);
                employeeService.Close();
                this.SetMessage(InfoStrings.EmployeeCreated, MessageType.Info);
                return RedirectToAction("Index");
            }
            catch (FaultException<ValidationException> ex)
            {
                CLogger.LogDebug(ex);
                this.SetMessage(ex.Detail.Message, MessageType.Error);
            }
            catch (Exception ex)
            {
                CLogger.LogDebug(ex);
                this.SetMessage(ErrorStrings.ErrorEmployeeCreating, MessageType.Error);
            }
            return View(employee);
        } 
        #endregion

        #region Edit
        //
        // GET: /Employee/Edit/5

        public ActionResult Edit(int id)
        {
            return Details(id);
        }

        //
        // POST: /Employee/Edit/5

        [HttpPost]
        public ActionResult Edit(int id, Employee employee, int contractId, int roleId, FormCollection form)
        {
            try
            {
                employee.Id = id;
                employee.Contract = new Contract { Id = contractId };
                employee.Role = new Role { Id = roleId };
                employee.Params = this.GetParameters(form, Model.Params.ParamType.Employee);
                EmployeeServiceClient employeeService = new EmployeeServiceClient();
                employeeService.AddValidationToken(this.Token);
                employeeService.StoreEmployee(employee);
                employeeService.Close();
                this.SetMessage(InfoStrings.EmployeeUpdated, MessageType.Info);
                return RedirectToAction("Index");
            }
            catch (FaultException<ValidationException> ex)
            {
                CLogger.LogDebug(ex);
                this.SetMessage(ex.Detail.Message, MessageType.Error);
            }
            catch (Exception ex)
            {
                CLogger.LogDebug(ex);
                this.SetMessage(ErrorStrings.ErrorEmployeeUpdating, MessageType.Error);
            }
            return View(employee);
        } 
        #endregion

        #region Delete
        //
        // GET: /Employee/Delete/5

        public ActionResult Delete(int id)
        {
            return Details(id);
        }

        //
        // POST: /Employee/Delete/5

        [HttpPost]
        public ActionResult Delete(int id, FormCollection collection)
        {
            try
            {
                EmployeeServiceClient employeeService = new EmployeeServiceClient();
                employeeService.AddValidationToken(this.Token);
                employeeService.DeleteEmployee(id);
                employeeService.Close();
                this.SetMessage(InfoStrings.EmployeeDeleted, MessageType.Info);
            }
            catch (FaultException<ValidationException> ex)
            {
                CLogger.LogDebug(ex);
                this.SetMessage(ex.Detail.Message, MessageType.Error);
            }
            catch (Exception ex)
            {
                CLogger.LogDebug(ex);
                this.SetMessage(ErrorStrings.ErrorEmployeeDeleting, MessageType.Error);
            }
            return RedirectToAction("Index");
        } 
        #endregion
    }
}
